<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Information;
use App\Models\Map;
use App\Models\Product;
use App\Models\Team;
use App\Services\ConfigService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Builder;

class InformationController extends AdminController
{
    /**
     * Make a grid builder.
     *
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make( Information::with('team'), function (Grid $grid) {
            $grid->model()->orderBy('created_at','desc');
            $grid->column('team.name','館所');
            $grid->column('nickname')->editable();
            $grid->column('height')->editable();
            $grid->column('weight')->editable();
            $grid->column('cup')->editable();
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->selector(function (Grid\Tools\Selector $selector){
                $selector->select('team_id', '館所', Team::pluck('name','id'));
            });
/*            $grid->showQuickEditButton();*/
            //$grid->disableEditButton();
            /*$grid->enableDialogCreate();*/
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Information(), function (Show $show) {
            $show->field('id');
            $show->field('nickname');
            $show->field('height');
            $show->field('weight');
            $show->field('cup');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Information(), function (Form $form) {
            $form->select('team_id','館所')->options(Team::where('status',1)->pluck('name','id'))->required();
            $form->text('nickname')->required();
            $form->decimal('price','價格')->required();
            $form->text('height')->required()->help('单位：cm');
            $form->text('weight')->required()->help('单位：kg');
            $form->radio('cup')->options([
                'B'=>'B','C'=>'C','D'=>'D','E'=>'E','F'=>'F','G'=>'G','H'=>'H'
            ])->required();
            $form->radio('scheduling','排班')->options([
                '早班','晚班','兼差PT'
            ])->required();

            $form->radio('is_secretly','明/暗配')->options([
                '1'=>'明配','2'=>'暗配'
            ])->default(0);
            $form->checkbox('prohibited_guests','禁客')->options(Product::PROHIBITED_GUESTS)->saving(function ($value) {
                return $value;
            });
            $form->radio('is_specific_service','預約送禮物')->options([
                1=>'有明確服務',2=>'無明確服務'
            ])->when(1,function (Form $form){
                $form->checkbox('gift_specific_service','預約送')->options(Product::SPECIFIC_SERVICE)->when(0,function (Form $form){
                    $form->text('custom_service','自定義贈送')->help('多個請用"|"分开');
                })->saving(function ($value) {
                    return $value;
                })->help('最多勾选3个');
            })->when(2,function (Form $form){
                $form->radio('gift_not_specific_service','預約送')->options(Product::SPECIFIC_NOT_SERVICE)->when(0,function (Form $form){
                    $form->text('custom_service2','自定義贈送');
                });
            })->default(0);

            $form->switch('status')->default(1);

            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if ($form->isEditing()) {
                    if($form->height && !is_numeric($form->height)){
                        return $form->response()->error('身高請填寫數值');
                    }
                    if($form->weight && !is_numeric($form->weight)){
                        return $form->response()->error('身高請填寫數值');
                    }
                    if($form->cup && !in_array($form->cup,['B','C','D','E','F','G','H'])){
                        return $form->response()->error('罩杯請填寫 B/C/D/E/F/G/H');
                    }

                    return;
                }

                // 修改操作
            });

        });
    }
}
